﻿using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;

public sealed class OleHelper
{
    private OleHelper() { }
    public static OleDbDataReader ExecuteReader(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
    {
        OleDbCommand cmd = new OleDbCommand();
        try
        {
            PrepareCommand(cmd, Connection, null, cmdType, cmdText, cmdParms);
            OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            cmd.Parameters.Clear();
            return rdr;
        }
        catch
        {
            //conn.Close();
            throw;
        }
    }
    public static OleDbDataReader ExecuteReader(string cmdText, params OleDbParameter[] cmdParms)
    {
        return ExecuteReader(CommandType.Text, cmdText, cmdParms);
    }


    public static object ExecuteScalar(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
    {
        OleDbCommand cmd = new OleDbCommand();

        PrepareCommand(cmd, Connection, null, cmdType, cmdText, cmdParms);
        object val = cmd.ExecuteScalar();
        cmd.Parameters.Clear();
        return val;
    }
    public static object ExecuteScalar(string cmdText, params OleDbParameter[] cmdParms)
    {
        return ExecuteScalar(CommandType.Text, cmdText, cmdParms);
    }


    public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
    {

        OleDbCommand cmd = new OleDbCommand();

        PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
        object val = cmd.ExecuteScalar();
        cmd.Parameters.Clear();
        return val;
    }



    private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
    {

        //if (conn.State != ConnectionState.Open)
        //    conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = cmdText;

        if (trans != null)
            cmd.Transaction = trans;

        cmd.CommandType = cmdType;

        if (cmdParms != null)
        {
            foreach (OleDbParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }
    public static void ExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParams)
    {
        OleDbCommand cmd = new OleDbCommand();
        PrepareCommand(cmd, Connection, null, cmdType, cmdText, cmdParams);
        object val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
    }
    public static void ExecuteNonQuery(string cmdText, params OleDbParameter[] cmdParams)
    {
        ExecuteNonQuery(CommandType.Text, cmdText, cmdParams);
    }
    public static DataSet ExecuteDataset(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParams)
    {
        OleDbCommand cmd = new OleDbCommand();
        PrepareCommand(cmd, Connection, null, cmdType, cmdText, cmdParams);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cmd.Parameters.Clear();
        return ds;
    }
    public static DataSet ExecuteDataset(string cmdText, params OleDbParameter[] cmdParams)
    {
        return ExecuteDataset(CommandType.Text, cmdText, cmdParams);
    }
    private static OleDbConnection connection;
    public static OleDbConnection Connection
    {
        get
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;
            if (connection == null)
            {
                connection = new OleDbConnection(connectionString);
                connection.Open();
            }
            else if (connection.State == System.Data.ConnectionState.Closed)
            {
                connection.Open();
            }
            else if (connection.State == System.Data.ConnectionState.Broken)
            {
                connection.Close();
                connection.Open();
            }
            return connection;
        }
    }
    public static OleDbConnection GetConnection()
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
        if (connection == null)
        {
            connection = new OleDbConnection(connectionString);
            connection.Open();
        }
        else if (connection.State == System.Data.ConnectionState.Closed)
        {
            connection.Open();
        }
        else if (connection.State == System.Data.ConnectionState.Broken)
        {
            connection.Close();
            connection.Open();
        }
        return connection;
    }
    public static OleDbParameter SetParameter(string name, object value)
    {
        return new OleDbParameter(name, value);
    }
}
